package cn.itsource.pet.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    public IPetService petService;

    /**
     * 接口：添加或修改
     *
     * @param pet 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request) {
        try {
            if (pet.getId() != null) {
                petService.update(pet);
            } else {
                petService.add(pet,request);
            }
            return new AjaxResult();
        } catch (Exception e) {
          e.printStackTrace();
          return new AjaxResult(false,"服务器内部错误【500】");
        }

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器内部错误【500】");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            petService.patchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器内部错误【500】");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Pet findOne(@PathVariable("id") Long id) {
        return petService.findOne(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<Pet> findAll() {
        return petService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageResult<Pet> queryPage(@RequestBody PetQuery query) {
        return petService.queryPage(query);
    }

    @PostMapping("/stateOne")
    public PageResult<Pet> stateOne(@RequestBody PetQuery query) {
        return petService.queryPage(query);
    }



    //批量修改宠物下架状态
    @PatchMapping("/grounding")
    public AjaxResult patchGrounding(@RequestBody Long[] ids) {
        try {
            return petService.patchGrounding(ids);
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器内部错误【500】");
        }
    }

    //批量修改宠物下架状态
    @PatchMapping("/offShelf")
    public AjaxResult offShelf(@RequestBody Long[] ids) {
        try {
            return petService.offShelf(ids);
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器内部错误【500】");
        }
    }

    @GetMapping("/getResources/{id}")
    public String getResources(@PathVariable("id") Long id){
        return petService.getResources(id);
    }

}
